/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
// 在递归的过程中直接求和就好
// 方法1：太菜了
var sumNumbers = function(root) {
    let path = []
    let res = 0
    const dfs = function(tnode) {
        if(tnode === null) return null
        path.push(tnode.val)
        let left = dfs(tnode.left)
        let right = dfs(tnode.right)
        if(left === null && right === null) {
            res += parseInt(path.join(''))
        }
        path.pop()
    }
    dfs(root)
    return res
};
// 方法2：递归过程中求和
var sumNumbers = function(root) {
    const dfs = function(tnode, curVal) {
        if(tnode === null) return 0
        let temp = curVal * 10 + tnode.val
        if(tnode.left === null && tnode.right === null) return temp
        return dfs(tnode.left, temp) + dfs(tnode.right, temp)
    }
    return dfs(root, 0)
};